package org.example.server.rest;

import org.example.server.model.core.User;
import org.example.server.model.request.LoginUserRequest;
import org.example.server.model.request.RegisterUserRequest;
import org.example.server.model.request.UpdateUserGenresRequest;
import org.example.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

//用于处理user相关的动作
@Controller
@RequestMapping("/rest/users")
public class UserRestApi {

    @Autowired
    private UserService userService;

    /**
     * 需要提供用户注册功能
     * url:  /rest/users/register?username=abc&password=abc
     *
     * @param username 用户名
     * @param password 密码
     * @param model
     * @return {success: true/false, userdata:{}}
     */
    @RequestMapping(path = "/register", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model registerUser(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
//        model.addAttribute("success", userService.registerUser(new RegisterUserRequest(username, password)));

        Boolean res =  userService.registerUser(new RegisterUserRequest(username, password));
        model.addAttribute("success",res);
        if(res == true){
            User user = userService.findUserByUsername(username);
            model.addAttribute("userdata", user);
        }
        return model;
    }

    /**
     * 需要提供用户登录功能
     * url:  /rest/users/login?username=abc&password=abc
     *
     * @param username
     * @param password
     * @param model
     * @return {success: true/false, userdata:{}}
     */
    @RequestMapping(path = "/login", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model loginUser(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        Boolean res =  userService.loginUser(new LoginUserRequest(username, password));
        model.addAttribute("success",res);
        if(res == true){
            User user = userService.findUserByUsername(username);
            model.addAttribute("userdata", user);
        }
        return model;
    }

    /**
     * 需要能够添加用户偏爱的影片类别
     * url:  /rest/users/genres?username=abc&genres=a|b|c
     *
     * @param username
     * @param genres
     * @param model
     */
    @RequestMapping(path = "/genres", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public void addGenres(@RequestParam("username") String username, @RequestParam("genres") String genres, Model model) {
        List<String> genresList = new ArrayList<String>();
        for (String gen : genres.split("\\|")) {
            genresList.add(gen);
        }

        userService.updateUserGenres(new UpdateUserGenresRequest(username, genresList));
    }
}
